package com.young.cms.web;

import com.young.cms.model.Column;
import com.young.cms.model.GenCol;
import com.young.cms.model.GenMain;
import com.young.cms.model.Table;
import com.young.cms.model.enums.*;
import com.young.cms.model.gen4.GenColV4;
import com.young.cms.model.gen4.GenMainV4;
import com.young.cms.service.ICmsDBService;
import com.young.common.model.html.DefaultKeyValueVO;
import com.young.common.model.html.IKeyValue;
import com.young.common.util.AssertUtil;
import com.young.common.util.Result;
import com.young.interfaces.log.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 代码生成V4
 *
 * @author wangjy
 * @date 2022/03/16 23:06:23
 */
@Controller
@RequestMapping(value = "/cms/genV4", method = {RequestMethod.GET, RequestMethod.POST})
public class CmsGenV4Controller {
    /**
     * db服务
     */
    @Autowired
    ICmsDBService cmsDBService;

    @Log("查询某表的字段信息")
    @RequestMapping(value = "/initGenByTableName", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<GenMainV4> initGenByTableName(String tableName) {
        // 查询表信息
        Table table = cmsDBService.getTableSimpleInfo(tableName);
        AssertUtil.notNull(table, "查询表信息为空");

        // 查询数据库字段
        List<Column> columnList = cmsDBService.getColumsByTableName(tableName);
        AssertUtil.notEmpty(columnList, "查询表字段为空");

        // 表字段映射为代码生成列配置
        List<GenColV4> genColList = columnList.stream().map(GenColV4::buildDefaultV4).collect(Collectors.toList());
        for (int i = 0; i < genColList.size(); i++) {
            GenColV4 genCol = genColList.get(i);
            genCol.setViewNum(i * 10 + 10);
            genCol.setFormNum(i * 10 + 10);
        }

        // 结果
        GenMainV4 genMain = GenMainV4.buildDefault(table);
        genMain.setColList(genColList);
        return Result.buildSuccess(genMain);
    }

    @Log("查询字段查询类型")
    @RequestMapping(value = "/getAsConditionTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<List<IKeyValue>> getAsConditionTypeList() {
        return Result.buildSuccess(Arrays.stream(EAsConditionTypeEnum.values()).map(item -> new DefaultKeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    @Log("查询展示格式类型")
    @RequestMapping(value = "/getShowFormatList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<List<IKeyValue>> getShowFormatList() {
        return Result.buildSuccess(Arrays.stream(EShowFormatEnum.values()).map(item -> new DefaultKeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    @Log("查询表格首列类型")
    @RequestMapping(value = "/getFirstColumnTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<List<IKeyValue>> getFirstColumnTypeList() {
        return Result.buildSuccess(Arrays.stream(ETableFirstColumnTypeEnum.values()).map(item -> new DefaultKeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    @Log("查询控件类型")
    @RequestMapping(value = "/getControlTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<List<IKeyValue>> getControlTypeList() {
        return Result.buildSuccess(Arrays.stream(EControlTypeEnum.values()).map(item -> new DefaultKeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    @Log("查询表单校验规则列表")
    @RequestMapping(value = "/getControlCheckList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<List<IKeyValue>> getControlCheckList() {
        return Result.buildSuccess(Arrays.stream(EControlCheckEnum.values()).map(item -> new DefaultKeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }
}
